package benny_driving_ser.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import atg.taglib.json.util.JSONException;
import atg.taglib.json.util.JSONObject;
import benny_driving_ser.dao.impl.AppVersionDaoImpl;
import benny_driving_ser.dao.impl.LoginDaoImpl;
import benny_driving_ser.entity.DriLogin;
import benny_driving_ser.entity.SessionAttributeDriId;
import benny_driving_ser.util.SessionUtil;

public class LoginServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8343899870695148495L;

	/**
	 * Constructor of the object.
	 */
	public LoginServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to
	 * post.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String action;
		
		if (null == (action = request.getParameter("action")))
			return;
		response.setContentType("text/html");
		if ("dri-getVerity".equals(action.trim())) {
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			PrintWriter out = response.getWriter();
			
			if (null != sad) {
				
				DriLogin dl = LoginDaoImpl.getInstance().driStateInfo(
						sad.getValue());
				JSONObject j = new JSONObject();
				try {
					j.put("isagr", dl.getIsagr());
					j.put("istopup", dl.getIstopup());
					j.put("state", dl.getState());
					j.put("pstate", dl.getPstate());
					j.put("ispay", dl.getIspay());
					j.put("serState", dl.getServerState());
				} catch (JSONException e) {
					e.printStackTrace();
				}
				out.print(j.toString());
				out.flush();
				out.close();
				return;
			}
			out.print(-1);
			out.flush();
			out.close();
		}
		if ("dri-login".equals(action.trim())) {
			String acc, psd,dri_version,dri_version_ios="";
			
			String last_version = AppVersionDaoImpl.getInstance().getDriVersion().getVersion();//CheckVersionDaoImpl.checkVersion(dri_version);
			PrintWriter out = response.getWriter();
			
			String last_version_ios = AppVersionDaoImpl.getInstance().getDriVersionIOS().getVersion();
			
			dri_version = request.getParameter("dri_version");
			dri_version_ios = request.getParameter("dri_version");
			
			if (null == (acc = request.getParameter("acc"))
					|| null == (psd = request.getParameter("psd")) 
					|| (null==dri_version && null ==dri_version_ios)){
				
				out.print(2);
				out.flush();
				out.close();
				return;
				}else if(dri_version.equals(last_version) || dri_version_ios.equals(last_version_ios)){
				SessionAttributeDriId sad = LoginDaoImpl.getInstance().driLogin(acc, psd);
				switch (Integer.parseInt(sad.getValue())) {
				case 1:
					break;
				case 2:
					break;
				default:
					SessionUtil.getInstance().getSessionId(request, response,
							new SessionAttributeDriId(sad.getValue(),sad.getPWDVersion()));
					sad.setValue("0");
					break;
				}
				out.print(sad.getValue());
				out.flush();
				out.close();
			}else{
				out.print(1);
				out.flush();
				out.close();
			}
		}
		
		if ("dri-qdxy".equals(action.trim())) {
			SessionAttributeDriId sad = SessionUtil.getInstance()
					.verifySessionIdForDri(request);
			int res = -1;
			if (null != sad)
				res = LoginDaoImpl.getInstance().signProtocol(0,
						Integer.parseInt(sad.getValue()));
			PrintWriter out = response.getWriter();
			out.print(res);
			out.flush();
			out.close();
		}

		if ("user-getcode".equals(action.trim())) {
			String tel;
			if (null == (tel = request.getParameter("tel")))
				return;
			int res = LoginDaoImpl.getInstance().vcReg(tel.trim());
			PrintWriter out = response.getWriter();
			out.print(res);
			out.flush();
			out.close();
		}

		if ("user-login".equals(action.trim())) {
			String acc, psd;
			if (null == (acc = request.getParameter("acc"))
					|| null == (psd = request.getParameter("psd")))
				return;
			int res = LoginDaoImpl.getInstance().userLogin(acc.trim(),
					psd.trim());
			PrintWriter out = response.getWriter();
			out.print(res);
			out.flush();
			out.close();
		}
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
